package com.example.demo.controller;

import com.example.demo.common.R;
import com.example.demo.common.RPage;
import com.example.demo.decorator.Authorize;
import com.example.demo.entity.sysConfig.*;
import com.example.demo.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys-config")
public class SysConfigController {
    @Autowired
    private SysConfigService sysConfigService;

    @GetMapping
    @Authorize(
            permission = "main::sysConfig:selList",
            label = "分页查询系统"
    )
    public R<RPage<SysConfig>> selSysConfig(@ModelAttribute SysConfigSelList sysConfigSelList) {
        return sysConfigService.selSysConfig(sysConfigSelList);
    }

    @GetMapping("/all")
    @Authorize(
            permission = "main::sysConfig:selAll",
            label = "查询所有系统"
    )
    public R<List<SysConfig>> selAllSysConfig(@ModelAttribute SysConfigSelAll sysConfigSelAll) {
        return sysConfigService.selAllSysConfig(sysConfigSelAll);
    }

    @GetMapping("/ids")
    @Authorize(
            permission = "main::sysConfig:selOnes",
            label = "查询多个系统（根据id）"
    )
    public R<List<SysConfig>> selOnesSysConfig(@RequestParam Map<String, String> ids) {
        return sysConfigService.selOnesSysConfig(ids.values().stream().map(Integer::valueOf).toList().toArray(new Integer[0]));
    }

    @GetMapping("/{id}")
    @Authorize(
            permission = "main::sysConfig:selOne",
            label = "查询单个系统"
    )
    public R<SysConfig> selOneSysConfig(@PathVariable Integer id) {
        return sysConfigService.selOneSysConfig(id);
    }

    @PostMapping
    @Authorize(
            permission = "main::sysConfig:ins",
            label = "新增系统"
    )
    public R<Boolean> insSysConfig(@RequestBody SysConfigInsOne sysConfigInsOne) {
        return sysConfigService.insSysConfig(sysConfigInsOne);
    }

    @PostMapping("/s")
    @Authorize(
            permission = "main::sysConfig:inss",
            label = "批量新增系统"
    )
    public R<Boolean> insSysConfigs(@RequestBody SysConfigInsOne[] sysConfigInsOnes) {
        return sysConfigService.insSysConfigs(sysConfigInsOnes);
    }

    @PutMapping
    @Authorize(
            permission = "main::sysConfig:upd",
            label = "修改系统"
    )
    public R<Boolean> updSysConfig(@RequestBody SysConfigUpdOne sysConfigUpdOne) {
        return sysConfigService.updSysConfig(sysConfigUpdOne);
    }

    @PutMapping("/s")
    @Authorize(
            permission = "main::sysConfig:upds",
            label = "批量修改系统"
    )
    public R<Boolean> updSysConfigs(@RequestBody SysConfigUpdOne[] sysConfigUpdOnes) {
        return sysConfigService.updSysConfigs(sysConfigUpdOnes);
    }

    @DeleteMapping
    @Authorize(
            permission = "main::sysConfig:del",
            label = "删除系统"
    )
    public R<Boolean> delSysConfig(@RequestBody Integer[] ids) {
        return sysConfigService.delSysConfig(ids);
    }
}
